﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using VideoClipNameHelper;

namespace GroupSimiliar
{
    public class VideoClipFile
    {
        public VideoClipData vcd;
        public string FileName { get; set; }
        public string GRoupName { get; set; }
    }
    //net use m: \\nedia_center\Video_AVI_Music /persistent:no
    //"c:\Visual Studio 2013\Projects\VideoClipNameHelper\GroupSimiliar\bin\Debug\GroupSimiliar.exe"

    class Program
    {
        private static List<string> SubDirs = new List<string>();
        private static bool isOnlyPrint = false;
        //
        private static List<VideoClipFile> vcf = new List<VideoClipFile>();
        //
        static void Main(string[] args)
        {
 //
            if(args.Length >0)
            {
                if (0 == string.Compare(args[0], "-OnlyPrint", true)) { isOnlyPrint = true; }
                else
                {
                    Console.WriteLine("Options:");
                    Console.WriteLine("-OnlyPrint : without moving files to directory");
                    return;
                }

            }
            //
            //
            foreach (string f in Directory.EnumerateFiles(".", "*.avi"))
            {
                VideoClipData vc = new VideoClipData();
                if (""==VideoClipData.Parse(Path.GetFileNameWithoutExtension(f), vc))
                {
                    vcf.Add(new VideoClipFile() { FileName = f, vcd = vc , GRoupName = vc.Artist.ToUpper()+"-"+vc.Title.ToUpper()});
                }
                else
                {
                    Console.WriteLine("ERR:" + f);
                }
            }
            //
            var grouped = (from x in vcf select x).GroupBy(x => x.GRoupName);
            foreach( var g in grouped)
            {
                if (g.Count() <= 1) continue;
                //
                Console.WriteLine("{0}", g.Key);
                foreach(var sf in g)
                {
                    Console.WriteLine("\t{0}", sf.FileName);
                    MoveFileToSubdir(sf.FileName, g.Key);
                }
                
                //
            }
            //
            //
        }









        private static bool MoveFileToSubdir(string f, string SubDirName)
        {
            SubDirName = SubDirName.ToUpper().Trim().Replace(' ', '_');
            //
            if (!SubDirs.Contains(SubDirName))
            {
                if (!Directory.Exists(SubDirName)) Directory.CreateDirectory(SubDirName);
                SubDirs.Add(SubDirName);
            }
            //
            Directory.Move(f, Path.Combine(SubDirName, f));
            //
            return true;
        }

    }///class
}//namespace
